<?php

namespace Xin\ThinkPHP\Util;

use think\Collection;
use think\Model;

class ModelUtil
{

    /**
     * 取出Pivot数据
     * @param Collection $result
     * @param callable $eachCallback
     * @return Collection
     */
    public static function carryPivots(Collection $result, callable $eachCallback)
    {
        return $result->each(function (Model $model) use ($eachCallback) {
            $eachCallback(static::carryPivot($model), $model);
        });
    }

    /**
     * 取出Pivot数据
     * @param Model $model
     * @return array
     */
    public static function carryPivot(Model $model)
    {
        $pivot = [];

        foreach ($model->getData() as $key => $val) {
            if (strpos($key, '__')) {
                [$name, $attr] = explode('__', $key, 2);

                if ('pivot' == $name) {
                    $pivot[$attr] = $val;
                    unset($model->$key);
                }
            }
        }

        return $pivot;
    }

}
